﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SharpScss;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Utils;
using SysFile = System.IO.File;

namespace vueweb.Controllers
{
	[Route("nodejs")]
	public class NodeJsController : BaseController
	{
		[DirectAccessAction]
		[Route("require")]
		public IActionResult Require([FromQuery] string dir, [FromQuery] bool sub, [FromQuery] string ext)
		{
			Dictionary<string, string> files = Get文件名称和文件内容(dir, sub, ext);
			string keys = JsonConvert.SerializeObject(files.Keys.ToList());
			string dirs = JsonConvert.SerializeObject(files, Formatting.None);
			string javascript = @$"
					const dirs = {dirs};
					const keys = {keys};
					function me(fkey){{ return dirs[fkey];}};
					me.keys = function(){{ return keys;}};
					export default me;

";
			return Content(javascript, "text/javascript");
		}

		public static Dictionary<string, string> Get文件名称和文件内容(string dir, bool includeSub, string fileExt)
		{
			string webRoot = Singleton<IWebHostEnvironment>.Instance.WebRootPath;
			string phyPath = Path.Combine(webRoot, dir);
			Dictionary<string, string> ret = new Dictionary<string, string>();
			string[] filepaths = Directory.GetFiles(phyPath, "*." + fileExt, includeSub ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
			if (filepaths.Count() == 0) return ret;
			foreach (string f in filepaths)
			{
				string content = SysFile.ReadAllText(f);
				string fkey = Path.GetRelativePath(webRoot, f);
				ret.Add(fkey, content);
			}
			return ret;
		}

		[DirectAccessAction]
		[Route("scss2css")]
		[HttpPost]
		public IActionResult ScssToCss([FromForm] string scss)
		{
			string css = Scss.ConvertToCss(scss, new ScssOptions() { OutputStyle = ScssOutputStyle.Compressed }).Css;
			return Content(css, "text/css");
		}

		[DirectAccessAction]
		[Route("importscssfile")]
		public IActionResult ImportScssFile([FromQuery] string file)
		{
			string webRoot = Singleton<IWebHostEnvironment>.Instance.WebRootPath;
			string phyPath = Path.IsPathRooted(file) ? file : Path.Combine(webRoot, file);

			string css = Scss.ConvertFileToCss(phyPath, new ScssOptions() { OutputStyle = ScssOutputStyle.Compressed }).Css;
			return Content(css, "text/css");
		}

		[DirectAccessAction]
		[Route("importscssobject")]
		public IActionResult ImportScssAsJs([FromQuery] string file)
		{
			string webRoot = Singleton<IWebHostEnvironment>.Instance.WebRootPath;
			string phyPath = Path.IsPathRooted(file) ? file : Path.Combine(webRoot, file);
			string content = System.IO.File.ReadAllText(phyPath);
			string css = Scss.ConvertToCss(content, new ScssOptions() { OutputStyle = ScssOutputStyle.Compact }).Css;
			string js = css.Replace(":export", "export default");
			Regex regex = new Regex(":\\s*([^:^;^\\s]+)[;}]");
			MatchCollection groups = regex.Matches(js);
			for (int i = 0; i < groups.Count; i++)
			{
				Match g = groups[i];
				js = js.Replace(g.Groups[0].Value, ":\"" + g.Groups[1].Value + "\"" + (i < groups.Count - 1 ? "," : ""));
			}
			return Content(js, "text/javascript");
		}
	}
}
